package com.codepoorfish.controller;

import com.codepoorfish.anno.UserLoginToken;
import com.codepoorfish.controller.utils.R;
import com.codepoorfish.domain.Activity;
import com.codepoorfish.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @UserLoginToken
    @GetMapping
    public R getActivity() {
        List<Activity> activities = activityService.list();
        if (activities == null) {
            return new R(false, "数据查找失败！");
        } else
            return new R(true, activities);
    }

    @GetMapping("/{id}")
    @UserLoginToken
    public R getActivityById(@PathVariable("id") Integer id) {
        Activity activity = activityService.getById(id);
        if (activity == null) {
            return new R(false, "数据查找失败！");
        } else
            return new R(true, activity);
    }

    // RestFul风格根据任务Id删除数据
    @DeleteMapping("/{id}")
    @UserLoginToken
    public R deleteActivityById(@PathVariable("id") Integer id) {
        return new R(activityService.removeById(id));
    }


    @DeleteMapping("/deleteBatch")
    @UserLoginToken
    public R deleteListActivity(@RequestBody List<String> ids) {
        return new R(activityService.removeByIds(ids));
    }

    // RestFul风格更新数据，参数为实体
    @PutMapping
    @UserLoginToken
    public R updateActivity(@RequestBody Activity activity) {
        return new R(activityService.updateById(activity));
    }

    // RestFul风格新增数据
    @PostMapping
    @UserLoginToken
    public R saveActivity(@RequestBody Activity activity) {
        return new R(activityService.save(activity));
    }
}
